Imports System.Xml
Namespace Data
  ''' <summary>
  ''' List of countries.
  ''' </summary>
  ''' <remarks>Data bindable.</remarks>
  Public Class CountryCollection
    Inherits System.Collections.ObjectModel.ReadOnlyCollection(Of Country)

    ''' <summary>
    ''' Converts a country name to country code or country code to country name.
    ''' </summary>
    ''' <param name="nameOrCode">Value to convert.</param>
    ''' <returns><see cref="String">Country name or code</see>.</returns>
    ''' <remarks></remarks>
    Public Shared Function ConvertNameOrCode(ByVal nameOrCode As String) As String

      If String.IsNullOrEmpty(nameOrCode) Then
        Throw New ArgumentNullException("nameOrCode")
      End If

      Dim countryResult As String = String.Empty

      'Get country info
      Dim countryData As New XmlDocument
      countryData.LoadXml(My.Resources.CountriesXML)

      Dim countryInfo As XmlNode = countryData.SelectSingleNode(String.Format(CultureInfo.InvariantCulture, "//ISO_3166-1_Entry[{0} = '{1}']", IIf(nameOrCode.Length = 2, "ISO_3166-1_Alpha-2_Code_element", "ISO_3166-1_Country_name"), nameOrCode.ToUpper(CultureInfo.InvariantCulture)))

      If Not IsNothing(countryInfo) AndAlso countryInfo.OuterXml.Length > 0 Then
        If nameOrCode.Length = 2 Then

          countryResult = countryInfo.SelectSingleNode("ISO_3166-1_Country_name").InnerText

        Else

          countryResult = countryInfo.SelectSingleNode("ISO_3166-1_Alpha-2_Code_element").InnerText

        End If

      End If

      Return countryResult

    End Function

    ''' <summary>
    ''' Constructor that auto loads countries list.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()
      MyBase.New(DataLists.CountriesList)
    End Sub

    ''' <summary>
    ''' Constructor that loads list of countries.
    ''' </summary>
    ''' <param name="list">List of <see cref="Country">Countries</see>.</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal list As System.Collections.Generic.IList(Of Country))
      MyBase.New(list)
    End Sub

  End Class
End Namespace
